home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectSound / VoiceManagement / voicemanagement.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  24.3 KB  |  681 lines

  1. //----------------------------------------------------------------------------
  2. // File: VoiceManagement.cpp
  3. //
  4. // Desc: Main application file for the VoiceManagement sample. 
  5. //
  6. // Copyright (c) 1999-2001 Microsoft Corp. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #define STRICT
  9. #include <windows.h>
  10. #include <basetsd.h>
  11. #include <commdlg.h>
  12. #include <commctrl.h>
  13. #include <mmreg.h>
  14. #include <dxerr8.h>
  15. #include <dsound.h>
  16. #include "resource.h"
  17. #include "DSUtil.h"
  18. #include "DXUtil.h"
  19.  
  20.  
  21.  
  22.  
  23. //-----------------------------------------------------------------------------
  24. // Function-prototypes
  25. //-----------------------------------------------------------------------------
  26. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam );
  27. VOID OnInitDialog( HWND hDlg );
  28. VOID OnOpenSoundFile( HWND hDlg );
  29. HRESULT OnPlaySound( HWND hDlg );
  30. VOID OnTimer( HWND hDlg );
  31. VOID EnablePlayUI( HWND hDlg, BOOL bShowPlayControl );
  32. VOID EnableManagementFlags( HWND hDlg, BOOL bShowFlags );
  33. VOID UpdateBehaviorText( HWND hDlg );
  34. VOID SetFileUI( HWND hDlg, TCHAR* strFileName );
  35.  
  36.  
  37.  
  38.  
  39. //-----------------------------------------------------------------------------
  40. // Defines, constants, and global variables
  41. //-----------------------------------------------------------------------------
  42. CSoundManager* g_pSoundManager = NULL;
  43. CSound*        g_pSound = NULL;
  44.  
  45.  
  46.  
  47.  
  48. //-----------------------------------------------------------------------------
  49. // Name: WinMain()
  50. // Desc: Entry point for the application.  Since we use a simple dialog for 
  51. //       user interaction we don't need to pump messages.
  52. //-----------------------------------------------------------------------------
  53. INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, 
  54.                       INT nCmdShow )
  55. {
  56.     InitCommonControls();
  57.  
  58.     // Display the main dialog box.
  59.     DialogBox( hInst, MAKEINTRESOURCE(IDD_MAIN), NULL, MainDlgProc );
  60.  
  61.     return TRUE;
  62. }
  63.  
  64.  
  65.  
  66.  
  67. //-----------------------------------------------------------------------------
  68. // Name: MainDlgProc()
  69. // Desc: Handles dialog messages
  70. //-----------------------------------------------------------------------------
  71. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  72. {
  73.     HRESULT hr;
  74.  
  75.     switch( msg ) 
  76.     {
  77.         case WM_INITDIALOG:
  78.             OnInitDialog( hDlg ); 
  79.             break;
  80.  
  81.         case WM_COMMAND:
  82.             switch( LOWORD(wParam) )
  83.             {
  84.                 case IDC_SOUNDFILE:
  85.                     OnOpenSoundFile( hDlg );
  86.                     break;
  87.  
  88.                 case IDCANCEL:
  89.                     EndDialog( hDlg, IDCANCEL );
  90.                     break;
  91.  
  92.                 case IDC_PLAY:
  93.                     // The 'play' button was pressed
  94.                     if( FAILED( hr = OnPlaySound( hDlg ) ) )
  95.                     {
  96.                         DXTRACE_ERR( TEXT("OnPlaySound"), hr );
  97.                         MessageBox( hDlg, "Error playing DirectSound buffer."
  98.                                     "Sample will now exit.", "DirectSound Sample", 
  99.                                     MB_OK | MB_ICONERROR );
  100.                         EndDialog( hDlg, IDABORT );
  101.                     }
  102.                     break;
  103.  
  104.                 case IDC_STOP:
  105.                     if( g_pSound )
  106.                     {
  107.                         g_pSound->Stop();
  108.                         g_pSound->Reset();
  109.                     }
  110.  
  111.                     EnablePlayUI( hDlg, TRUE );
  112.                     break;
  113.  
  114.                 case IDC_ALLOC_HARDWARE:
  115.                 case IDC_ALLOC_EITHER:
  116.                     EnableManagementFlags( hDlg, TRUE );
  117.                     UpdateBehaviorText( hDlg );
  118.                     break;
  119.  
  120.                 case IDC_ALLOC_SOFTWARE:
  121.                     EnableManagementFlags( hDlg, FALSE );
  122.                     UpdateBehaviorText( hDlg );
  123.                     break;
  124.  
  125.                 case IDC_BYTIME:
  126.                     if( IsDlgButtonChecked( hDlg, IDC_BYTIME ) == BST_CHECKED ) 
  127.                         CheckDlgButton( hDlg, IDC_BYDISTANCE, BST_UNCHECKED );
  128.                     UpdateBehaviorText( hDlg );
  129.                     break;
  130.  
  131.                 case IDC_BYDISTANCE:
  132.                     if( IsDlgButtonChecked( hDlg, IDC_BYDISTANCE ) == BST_CHECKED ) 
  133.                         CheckDlgButton( hDlg, IDC_BYTIME, BST_UNCHECKED );
  134.                     UpdateBehaviorText( hDlg );
  135.                     break;
  136.                     
  137.                 case IDC_BYPRIORTY:
  138.                     UpdateBehaviorText( hDlg );
  139.                     break;
  140.  
  141.                 default:
  142.                     return FALSE; // Didn't handle message
  143.             }
  144.             break;
  145.  
  146.         case WM_TIMER:
  147.             OnTimer( hDlg );
  148.             break;
  149.  
  150.         case WM_DESTROY:
  151.             // Cleanup everything
  152.             KillTimer( hDlg, 1 );    
  153.             SAFE_DELETE( g_pSound );
  154.             SAFE_DELETE( g_pSoundManager );
  155.             break; 
  156.  
  157.         default:
  158.             return FALSE; // Didn't handle message
  159.     }
  160.  
  161.     return TRUE; // Handled message
  162. }
  163.  
  164.  
  165.  
  166.  
  167. //-----------------------------------------------------------------------------
  168. // Name: OnInitDialog()
  169. // Desc: Initializes the dialogs (sets up UI controls, etc.)
  170. //-----------------------------------------------------------------------------
  171. VOID OnInitDialog( HWND hDlg )
  172. {
  173.     HRESULT hr;
  174.  
  175.     // Load the icon
  176. #ifdef _WIN64
  177.     HINSTANCE hInst = (HINSTANCE) GetWindowLongPtr( hDlg, GWLP_HINSTANCE );
  178. #else
  179.     HINSTANCE hInst = (HINSTANCE) GetWindowLong( hDlg, GWL_HINSTANCE );
  180. #endif
  181.     HICON hIcon = LoadIcon( hInst, MAKEINTRESOURCE( IDR_MAINFRAME ) );
  182.  
  183.     // Create a static IDirectSound in the CSound class.  
  184.     // Set coop level to DSSCL_PRIORITY, and set primary buffer 
  185.     // format to stereo, 22kHz and 16-bit output.
  186.     g_pSoundManager = new CSoundManager();
  187.  
  188.     if( FAILED( hr = g_pSoundManager->Initialize( hDlg, DSSCL_PRIORITY, 2, 22050, 16 ) ) )
  189.     {
  190.         DXTRACE_ERR( TEXT("Initialize"), hr );
  191.         MessageBox( hDlg, "Error initializing DirectSound.  Sample will now exit.", 
  192.                             "DirectSound Sample", MB_OK | MB_ICONERROR );
  193.         EndDialog( hDlg, IDABORT );
  194.         return;
  195.     }
  196.  
  197.     // Check the 'hardware' voice allocation button by default. 
  198.     CheckRadioButton( hDlg, IDC_ALLOC_EITHER, IDC_ALLOC_SOFTWARE, IDC_ALLOC_EITHER );
  199.  
  200.     HWND hEditPri = GetDlgItem( hDlg, IDC_EDIT_PRIORITY );
  201.     HWND hSpinPri = GetDlgItem( hDlg, IDC_SPIN_PRIORITY );
  202.     SendMessage( hSpinPri, UDM_SETBUDDY, (WPARAM) hEditPri, 0 );
  203.     SendMessage( hSpinPri, UDM_SETRANGE, 0, MAKELONG (0x7FFF, 0) );
  204.     SendMessage( hSpinPri, UDM_SETPOS, 0, 0 );
  205.     SendMessage( hEditPri, EM_LIMITTEXT, 5, 0 );
  206.  
  207.     // Set the icon for this dialog.
  208.     PostMessage( hDlg, WM_SETICON, ICON_BIG,   (LPARAM) hIcon );  // Set big icon
  209.     PostMessage( hDlg, WM_SETICON, ICON_SMALL, (LPARAM) hIcon );  // Set small icon
  210.  
  211.     // Create a timer, so we can check for when the soundbuffer is stopped
  212.     SetTimer( hDlg, 0, 250, NULL );
  213.  
  214.     // Set the UI controls
  215.     UpdateBehaviorText( hDlg );
  216.     SetDlgItemText( hDlg, IDC_FILENAME, TEXT("No file loaded.") );
  217. }
  218.  
  219.  
  220.  
  221.  
  222. //-----------------------------------------------------------------------------
  223. // Name: OnOpenSoundFile()
  224. // Desc: Called when the user requests to open a sound file
  225. //-----------------------------------------------------------------------------
  226. VOID OnOpenSoundFile( HWND hDlg ) 
  227. {
  228.     HRESULT hr;
  229.  
  230.     static TCHAR strFileName[MAX_PATH] = TEXT("");
  231.     static TCHAR strPath[MAX_PATH] = TEXT("");
  232.  
  233.     // Setup the OPENFILENAME structure
  234.     OPENFILENAME ofn = { sizeof(OPENFILENAME), hDlg, NULL,
  235.                          TEXT("Wave Files\0*.wav\0All Files\0*.*\0\0"), NULL,
  236.                          0, 1, strFileName, MAX_PATH, NULL, 0, strPath,
  237.                          TEXT("Open Sound File"),
  238.                          OFN_FILEMUSTEXIST|OFN_HIDEREADONLY, 0, 0,
  239.                          TEXT(".wav"), 0, NULL, NULL };
  240.  
  241.     // Get the default media path (something like C:\WINDOWS\MEDIA)
  242.     if( '\0' == strPath[0] )
  243.     {
  244.         GetWindowsDirectory( strPath, MAX_PATH );
  245.         if( strcmp( &strPath[strlen(strPath)], TEXT("\\") ) )
  246.             strcat( strPath, TEXT("\\") );
  247.         strcat( strPath, TEXT("MEDIA") );
  248.     }
  249.  
  250.     if( g_pSound )
  251.     {
  252.         g_pSound->Stop();
  253.         g_pSound->Reset();
  254.     }
  255.  
  256.     // Update the UI controls to show the sound as loading a file
  257.     EnableWindow(  GetDlgItem( hDlg, IDC_PLAY ), FALSE);
  258.     EnableWindow(  GetDlgItem( hDlg, IDC_STOP ), FALSE);
  259.     SetDlgItemText( hDlg, IDC_FILENAME, TEXT("Loading file...") );
  260.  
  261.     // Display the OpenFileName dialog. Then, try to load the specified file
  262.     if( TRUE != GetOpenFileName( &ofn ) )
  263.     {
  264.         SetDlgItemText( hDlg, IDC_FILENAME, TEXT("Load aborted.") );
  265.         return;
  266.     }
  267.  
  268.     SetDlgItemText( hDlg, IDC_FILENAME, TEXT("") );
  269.  
  270.     // Free any previous sound, and make a new one
  271.     SAFE_DELETE( g_pSound );
  272.  
  273.     // Load the wave file into a DirectSound buffer
  274.     if( FAILED( hr = g_pSoundManager->Create( &g_pSound, strFileName, 
  275.                                          DSBCAPS_LOCDEFER, GUID_NULL ) ) )
  276.     {
  277.         // Not a critical failure, so just update the status
  278.         DXTRACE_ERR_NOMSGBOX( TEXT("Create"), hr );
  279.         SetDlgItemText( hDlg, IDC_FILENAME, TEXT("Could not create sound buffer.") );
  280.         return; 
  281.     }
  282.  
  283.     // Update the UI controls to show the sound as the file is loaded
  284.     SetDlgItemText( hDlg, IDC_FILENAME, strFileName );
  285.     EnablePlayUI( hDlg, TRUE );
  286.  
  287.     // Remember the path for next time
  288.     strcpy( strPath, strFileName );
  289.     char* strLastSlash = strrchr( strPath, '\\' );
  290.     strLastSlash[0] = '\0';
  291. }
  292.  
  293.  
  294.  
  295.  
  296. //-----------------------------------------------------------------------------
  297. // Name: OnPlaySound()
  298. // Desc: User hit the "Play" button
  299. //-----------------------------------------------------------------------------
  300. HRESULT OnPlaySound( HWND hDlg ) 
  301. {
  302.     HRESULT hr;
  303.     LONG    lPriority;
  304.     DWORD   dwPlayFlags;
  305.     BOOL    bLooped;
  306.     BOOL    bAllocHW;
  307.     BOOL    bAllocSW;
  308.     BOOL    bAllocEither;
  309.     BOOL    bByTime;
  310.     BOOL    bByDistance;
  311.     BOOL    bByPriority;
  312.  
  313.     bLooped = ( IsDlgButtonChecked( hDlg, IDC_LOOP_CHECK ) == BST_CHECKED );
  314.  
  315.     // Determine where the buffer would like to be allocated 
  316.     bAllocHW     = ( IsDlgButtonChecked( hDlg, IDC_ALLOC_HARDWARE ) == BST_CHECKED );
  317.     bAllocSW     = ( IsDlgButtonChecked( hDlg, IDC_ALLOC_SOFTWARE ) == BST_CHECKED );
  318.     bAllocEither = ( IsDlgButtonChecked( hDlg, IDC_ALLOC_EITHER   ) == BST_CHECKED );
  319.  
  320.     if( bAllocHW || bAllocEither )
  321.     {
  322.         // Determine how the buffer should steal hardware resources (if they are not available)
  323.         bByTime      = ( IsDlgButtonChecked( hDlg, IDC_BYTIME     ) == BST_CHECKED );
  324.         bByDistance  = ( IsDlgButtonChecked( hDlg, IDC_BYDISTANCE ) == BST_CHECKED );
  325.         bByPriority  = ( IsDlgButtonChecked( hDlg, IDC_BYPRIORTY  ) == BST_CHECKED );
  326.     }
  327.     else
  328.     {
  329.         // Buffers running in software are not allowed to have
  330.         // voice management flags since they have no need to 
  331.         // steal hardware resources.
  332.         bByTime      = FALSE;
  333.         bByDistance  = FALSE;
  334.         bByPriority  = FALSE;
  335.     }
  336.  
  337.     // Get the buffer priority
  338.     TCHAR strText[MAX_PATH];
  339.     GetDlgItemText( hDlg, IDC_EDIT_PRIORITY, strText, MAX_PATH );
  340.     lPriority = atol( strText );
  341.  
  342.     if( lPriority < 0 || lPriority > 32767 )
  343.     {
  344.         MessageBox( hDlg, "Please enter a buffer priority between 0 and 32767", 
  345.                     "DirectSound Sample", MB_OK );
  346.         return S_OK;
  347.     }
  348.  
  349.     // Figure out the voice allocation flag from the dialog,
  350.     // and what the user should expect based on the dialog choice
  351.     if( bAllocSW )
  352.         dwPlayFlags = DSBPLAY_LOCSOFTWARE;
  353.  
  354.     if( bAllocHW )
  355.         dwPlayFlags = DSBPLAY_LOCHARDWARE;
  356.  
  357.     if( bAllocEither )
  358.         dwPlayFlags = 0;
  359.  
  360.     // Figure out what voice management flags should be based on the dlg
  361.     if( bByTime )
  362.     {
  363.         if( bByPriority )
  364.         {
  365.             dwPlayFlags |= DSBPLAY_TERMINATEBY_TIME | 
  366.                            DSBPLAY_TERMINATEBY_PRIORITY;
  367.         }
  368.         else
  369.         {
  370.             dwPlayFlags |= DSBPLAY_TERMINATEBY_TIME;
  371.         }
  372.     }
  373.     else if( bByDistance )
  374.     {
  375.         if( bByPriority )
  376.         {
  377.             dwPlayFlags |= DSBPLAY_TERMINATEBY_DISTANCE | 
  378.                            DSBPLAY_TERMINATEBY_PRIORITY;
  379.         }
  380.         else
  381.         {
  382.             dwPlayFlags |= DSBPLAY_TERMINATEBY_DISTANCE;
  383.         }
  384.     }
  385.     else
  386.     {
  387.         if( bByPriority )
  388.         {
  389.             dwPlayFlags |= DSBPLAY_TERMINATEBY_PRIORITY;
  390.         }
  391.         else
  392.         {
  393.             dwPlayFlags |= 0;
  394.         }
  395.     }
  396.  
  397.  
  398.     if( bLooped )
  399.         dwPlayFlags |= DSBPLAY_LOOPING;
  400.  
  401.     // Play the sound 
  402.     if( FAILED( hr = g_pSound->Play( lPriority, dwPlayFlags ) ) )
  403.     {
  404.         if( hr == DSERR_CONTROLUNAVAIL || 
  405.             hr == DSERR_INVALIDCALL ||
  406.             hr == E_FAIL ||
  407.             hr == E_NOTIMPL)
  408.         {
  409.             DXTRACE_ERR_NOMSGBOX( TEXT("Play"), hr );
  410.             if( hr == DSERR_INVALIDCALL )
  411.             {
  412.                 MessageBox( hDlg, "Unsupported wave file format.", 
  413.                             "DirectPlay Sample", MB_OK | MB_ICONERROR );
  414.             }
  415.             else
  416.             {
  417.                 MessageBox( hDlg, "The buffer could not be played.", 
  418.                             "DirectPlay Sample", MB_OK | MB_ICONERROR );
  419.             }
  420.  
  421.             return S_OK;
  422.         }
  423.        
  424.         return DXTRACE_ERR( TEXT("Play"), hr );
  425.     }
  426.  
  427.     // Update the UI controls to show the sound as playing
  428.     EnablePlayUI( hDlg, FALSE );
  429.  
  430.     return S_OK;
  431. }
  432.  
  433.  
  434.  
  435.  
  436. //-----------------------------------------------------------------------------
  437. // Name: OnTimer()
  438. // Desc: When we think the sound is playing this periodically checks to see if 
  439. //       the sound has stopped.  If it has then updates the dialog.
  440. //-----------------------------------------------------------------------------
  441. VOID OnTimer( HWND hDlg ) 
  442. {
  443.     if( IsWindowEnabled( GetDlgItem( hDlg, IDC_STOP ) ) )
  444.     {
  445.         // We think the sound is playing, so see if it has stopped yet.
  446.         if( !g_pSound->IsSoundPlaying() ) 
  447.         {
  448.             // Update the UI controls to show the sound as stopped
  449.             EnablePlayUI( hDlg, TRUE );
  450.         }
  451.     }
  452. }
  453.  
  454.  
  455.  
  456.  
  457. //-----------------------------------------------------------------------------
  458. // Name: UpdateBehaviorText()
  459. // Desc: Figure out what the expected behavoir is based on the dialog,
  460. //       and display it on the dialog
  461. //-----------------------------------------------------------------------------
  462. VOID UpdateBehaviorText( HWND hDlg )
  463. {
  464.     TCHAR   strExcepted[1024];
  465.     BOOL    bAllocHW;
  466.     BOOL    bAllocSW;
  467.     BOOL    bAllocEither;
  468.     BOOL    bByTime;
  469.     BOOL    bByDistance;
  470.     BOOL    bByPriority;
  471.  
  472.     // Determine where the buffer would like to be allocated 
  473.     bAllocHW     = ( IsDlgButtonChecked( hDlg, IDC_ALLOC_HARDWARE ) == BST_CHECKED );
  474.     bAllocSW     = ( IsDlgButtonChecked( hDlg, IDC_ALLOC_SOFTWARE ) == BST_CHECKED );
  475.     bAllocEither = ( IsDlgButtonChecked( hDlg, IDC_ALLOC_EITHER   ) == BST_CHECKED );
  476.  
  477.     if( bAllocHW || bAllocEither )
  478.     {
  479.         // Determine how the buffer should steal hardware resources (if they are not available)
  480.         bByTime      = ( IsDlgButtonChecked( hDlg, IDC_BYTIME     ) == BST_CHECKED );
  481.         bByDistance  = ( IsDlgButtonChecked( hDlg, IDC_BYDISTANCE ) == BST_CHECKED );
  482.         bByPriority  = ( IsDlgButtonChecked( hDlg, IDC_BYPRIORTY  ) == BST_CHECKED );
  483.     }
  484.     else
  485.     {
  486.         // Buffers running in software are not allowed to have
  487.         // voice management flags since they have no need to 
  488.         // steal hardware resources.
  489.         bByTime      = FALSE;
  490.         bByDistance  = FALSE;
  491.         bByPriority  = FALSE;
  492.     }
  493.  
  494.     // Figure what the user should expect based on the dialog choice
  495.     if( bAllocSW )
  496.     {
  497.         strcpy( strExcepted, "The new sound will be played in software" );
  498.     }
  499.  
  500.     if( bAllocHW )
  501.     {
  502.         strcpy( strExcepted, "The new sound will be played in hardware" );
  503.     }
  504.  
  505.     if( bAllocEither )
  506.     {
  507.         strcpy( strExcepted, "The new sound will be played in hardware "
  508.                              "if available" );
  509.     }
  510.  
  511.     if( bByTime )
  512.     {
  513.         if( bByPriority )
  514.         {
  515.             if( bAllocEither )
  516.             {
  517.                 strcpy( strExcepted, "The new sound will be played in hardware, "
  518.                                      "if the the hardware has no available "
  519.                                      "voices, and new sound has a higher priority "
  520.                                      "than sounds currently playing in hardware "
  521.                                      "then sound with the lowest priority will be "
  522.                                      "terminated and the new sound will play in "
  523.                                      "hardware. Otherwise, the new sound will play "
  524.                                      "in software.  In event of a priority tie, "
  525.                                      "then the buffer with the least time left to "
  526.                                      "play will be prematurely terminated." );
  527.             }
  528.             else
  529.             {
  530.                 strcat( strExcepted, ", and if the hardware has no available "
  531.                                      "voices, the voice management buffer with "
  532.                                      "the lowest priority as set by the "
  533.                                      "IDirectSoundBuffer::Play priority argument "
  534.                                      "will be prematurely terminated. In event "
  535.                                      "of a priority tie, then the buffer with "
  536.                                      "the least time left to play will be "
  537.                                      "prematurely terminated." );         
  538.             }
  539.         }
  540.         else
  541.         {
  542.             strcat( strExcepted, ", and if the hardware has no available "
  543.                                  "voices, the voice management buffer with "
  544.                                  "the least time left to play will be "
  545.                                  "prematurely terminated." );                                     
  546.         }
  547.     }
  548.     else if( bByDistance )
  549.     {
  550.         if( bByPriority )
  551.         {
  552.             if( bAllocEither )
  553.             {
  554.                 strcpy( strExcepted, "The new sound will be played in hardware, "
  555.                                      "if the the hardware has no available "
  556.                                      "voices, and new sound has a higher priority "
  557.                                      "than sounds currently playing in hardware "
  558.                                      "then sound with the lowest priority will be "
  559.                                      "terminated and the new sound will play in "
  560.                                      "hardware. Otherwise, the new sound will play "
  561.                                      "in software.  In event of a priority tie, "
  562.                                      "then the buffer which is the furthest "
  563.                                      "distance from the listener at the time "
  564.                                      "of the Play will be prematurely terminated." );
  565.             }
  566.             else
  567.             {
  568.                 strcat( strExcepted, ", and if the hardware has no available "
  569.                                      "voices, the voice management buffer with "
  570.                                      "the lowest priority as set by the "
  571.                                      "IDirectSoundBuffer::Play priority argument "
  572.                                      "will be prematurely terminated. In event "
  573.                                      "of a priority tie, then the buffer which "
  574.                                      "is the furthest distance from the "
  575.                                      "listener at the time of the Play will "
  576.                                      "be prematurely terminated." );
  577.             }
  578.         }
  579.         else
  580.         {
  581.             strcat( strExcepted, ", and if the hardware has no available "
  582.                                  "voices, the voice management buffer which "
  583.                                  "is the furthest distance from the "
  584.                                  "listener at the time of the Play will "
  585.                                  "be prematurely terminated." );
  586.  
  587.         }
  588.     }
  589.     else
  590.     {
  591.         if( bByPriority )
  592.         {
  593.             if( bAllocEither )
  594.             {
  595.                 strcpy( strExcepted, "The new sound will be played in hardware, "
  596.                                      "if the the hardware has no available "
  597.                                      "voices, and new sound has a higher priority "
  598.                                      "than sounds currently playing in hardware "
  599.                                      "then sound with the lowest priority will be "
  600.                                      "terminated and the new sound will play in "
  601.                                      "hardware. Otherwise, the new sound will play "
  602.                                      "in software." );
  603.             }
  604.             else
  605.             {
  606.                 strcat( strExcepted, ", and if the hardware has no available "
  607.                                      "voices, the voice management buffer with "
  608.                                      "the lowest priority as set by the "
  609.                                      "IDirectSoundBuffer::Play priority argument "
  610.                                      "will be prematurely terminated. " );
  611.             }
  612.         }
  613.         else
  614.         {
  615.             strcat( strExcepted, ", and the buffer will not steal any "
  616.                                  "hardware resources." );
  617.         }
  618.     }
  619.  
  620.  
  621.     // Tell the user what to expect
  622.     SetDlgItemText( hDlg, IDC_BEHAVIOR, strExcepted );
  623. }
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630. //-----------------------------------------------------------------------------
  631. // Name: EnablePlayUI()
  632. // Desc: Enables or disables the Play UI controls 
  633. //-----------------------------------------------------------------------------
  634. VOID EnablePlayUI( HWND hDlg, BOOL bShowPlayControl )
  635. {
  636.     EnableWindow( GetDlgItem( hDlg, IDC_LOOP_CHECK ),  bShowPlayControl );
  637.     EnableWindow( GetDlgItem( hDlg, IDC_STOP ),       !bShowPlayControl );
  638.     EnableWindow( GetDlgItem( hDlg, IDC_PLAY ),        bShowPlayControl );
  639.  
  640.     // Don't allow the voice allocation or voicemanagement flags 
  641.     // to be changed when a sound is playing
  642.     EnableWindow( GetDlgItem( hDlg, IDC_BYTIME         ), bShowPlayControl );
  643.     EnableWindow( GetDlgItem( hDlg, IDC_BYDISTANCE     ), bShowPlayControl );
  644.     EnableWindow( GetDlgItem( hDlg, IDC_BYPRIORTY      ), bShowPlayControl );
  645.     EnableWindow( GetDlgItem( hDlg, IDC_EDIT_PRIORITY  ), bShowPlayControl );
  646.     EnableWindow( GetDlgItem( hDlg, IDC_ALLOC_HARDWARE ), bShowPlayControl );
  647.     EnableWindow( GetDlgItem( hDlg, IDC_ALLOC_SOFTWARE ), bShowPlayControl );
  648.     EnableWindow( GetDlgItem( hDlg, IDC_ALLOC_EITHER   ), bShowPlayControl );
  649.  
  650.     if( bShowPlayControl )
  651.     {
  652.         // If the software alloc flag is checked, then don't enable
  653.         // the voice management flags
  654.         if( IsDlgButtonChecked( hDlg, IDC_ALLOC_SOFTWARE ) == BST_CHECKED )
  655.             EnableManagementFlags( hDlg, FALSE );
  656.     }
  657.  
  658.     if( bShowPlayControl )
  659.         SetFocus( GetDlgItem( hDlg, IDC_PLAY ) );
  660.     else
  661.         SetFocus( GetDlgItem( hDlg, IDC_STOP ) );
  662. }
  663.  
  664.  
  665.  
  666.  
  667. //-----------------------------------------------------------------------------
  668. // Name: EnableManagementFlags()
  669. // Desc: Enable or disable the voice management flags
  670. //-----------------------------------------------------------------------------
  671. VOID EnableManagementFlags( HWND hDlg, BOOL bShowFlags )
  672. {
  673.     EnableWindow( GetDlgItem( hDlg, IDC_BYTIME        ), bShowFlags );
  674.     EnableWindow( GetDlgItem( hDlg, IDC_BYDISTANCE    ), bShowFlags );
  675.     EnableWindow( GetDlgItem( hDlg, IDC_BYPRIORTY     ), bShowFlags );
  676.     EnableWindow( GetDlgItem( hDlg, IDC_EDIT_PRIORITY ), bShowFlags );
  677. }
  678.  
  679.  
  680.  
  681.